package de.fhkl.thatsstroke.utilities;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Helper Klasse
 * 	<br>- md5 Hash Generierung
 *  <br>- byte in string umwandeln
 */
public class Utilities
{	
	private static String convertByteToString(byte[] array)
	{
		StringBuffer hexString = new StringBuffer();
		
        for (int i = 0; i < array.length; i++)
        {
            if((Integer.toHexString(0xFF & array[i])).length() < 2)
            {
            	hexString.append("0" + Integer.toHexString(0xFF & array[i]));
            }
            else
            {
            	hexString.append(Integer.toHexString(0xFF & array[i]));
            }
        }
        
		return hexString.toString();
	}

	public static String generateMd5Sum(String plain)
	{
		try
		{
			MessageDigest md;
			md = MessageDigest.getInstance("MD5");
			md.reset();
			md.update(plain.getBytes());
			return convertByteToString(md.digest());
		}
		catch (NoSuchAlgorithmException e)
		{
			e.printStackTrace();
		}
		
		return null;
	}
}
